package com.qxf.service.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.qxf.dao.LoginDao;
import com.qxf.exception.UserConfigurationException;
import com.qxf.model.ReturnEvent;
import com.qxf.model.beans.UserSession;
import com.qxf.service.BaseAction;
import com.qxf.util.Constants;

public class LoginAction extends BaseAction {

	private LoginDao loginDao;
	
	Logger log = Logger.getLogger(this.getClass());
	
	@Override
	public ReturnEvent process(HttpServletRequest req, HttpServletResponse resp) {
		String op = req.getParameter("op");
		log.info("op = " + op);
		ReturnEvent responseEvent = new ReturnEvent();
		Object data = null;
		loginDao = new LoginDao();
		try {
			if("CheckLogin".equalsIgnoreCase(op)) {
				data = checkLogin(req, resp);
			} else if("checkInActivity".equalsIgnoreCase(op)) {
				data = checkInActivity(req, resp);
			} else if("login".equalsIgnoreCase(op)) {
				data = login(req, resp);
			} else if("logout".equalsIgnoreCase(op)) {
				data = logout(req, resp);
			} else if("changeActivity".equalsIgnoreCase(op)) {
				data = changeActivity(req, resp);
			}
		} catch(UserConfigurationException uce) {
			responseEvent.setReturnCode(Constants.RETURN_FAILURE);
			responseEvent.setErrorMessage(uce.getMessage());
			log.info("UserConfigurationException: " + uce.getMessage());
		} catch (Exception e) {
			responseEvent.setReturnCode(Constants.RETURN_FAILURE);
			e.printStackTrace();
		}
		if(data != null) {
			responseEvent.setReturnCode(Constants.RETURN_SUCCESS);
			responseEvent.setResult(data);
		} else {
			responseEvent.setReturnCode(Constants.RETURN_FAILURE);
		}
		return responseEvent;
	}
	
	private UserSession checkInActivity(HttpServletRequest req, HttpServletResponse resp) {
		UserSession userInfo = (UserSession)req.getSession().getAttribute(Constants.SESSION_KEY);
		if(null != userInfo && userInfo.isInActivity()) {
			return userInfo;
		}
		return null;
	}
	
	private UserSession checkLogin(HttpServletRequest req, HttpServletResponse resp) {
		UserSession userInfo = (UserSession)req.getSession().getAttribute(Constants.SESSION_KEY);
		if(null != userInfo && userInfo.isLogin()) {
			return userInfo;
		}
		return null;
	}
	
	private UserSession login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String userName = req.getParameter("username");
		String passwd = req.getParameter("passwd");
		log.info("login() userName=" + userName + ", password=" + passwd);
		
		UserSession userInfo = loginDao.login(userName, passwd);
		if(userInfo != null && userInfo.isLogin()) {
			req.getSession().setAttribute(Constants.SESSION_KEY, userInfo);
			return userInfo;
		}
		return null;
	}
	
	private String logout(HttpServletRequest req, HttpServletResponse resp) {
		try {
			req.getSession().removeAttribute(Constants.SESSION_KEY);
			return Constants.RETURN_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private String changeActivity(HttpServletRequest req, HttpServletResponse resp) {
		try {
			String index = req.getParameter("index");
			log.debug("changeActivity(): index=" + index);
			if(null != index) {
				UserSession userSession = (UserSession)req.getSession().getAttribute(Constants.SESSION_KEY);
				userSession.setActivityInfo(userSession.getActivityInfoList().get(Integer.parseInt(index)));
				req.getSession().setAttribute(Constants.SESSION_KEY, userSession);
				return Constants.RETURN_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
